<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鸭子类型</title>
</head>

<body>
    <script>
        var duck = {
            duckString: function () {
                console.log('this is a duck');
            },
            soundString: 'dddddd'
        }
        var chicken = {
            duckString: function () {
                console.log('this is a chicken');
            },
            soundString: function () {
                console.log("cccc");
            }
        }

        var choir = [];//合唱团
        var joinTheChoir = function (animal) {
            console.log('aaa');
            console.log(animal, typeof animal.duckString);
            if (animal && typeof animal.duckString === 'function') {
                choir.push(animal);
                console.log('恭喜加入合唱团');
                console.log('合唱团成员数量：' + choir.length);
            }
        }

        //使用
        joinTheChoir(duck);
        joinTheChoir(chicken);
    </script>
</body>

</html>